package com.example.leetcode.trainingcamp.week5.Thursday;

/**
 * 符合下列属性的数组 arr 称为 山脉数组 ： arr.length >= 3 存在 i（0 < i < arr.length - 1）使得： arr[0] < arr[1] < ...
 * arr[i-1] < arr[i] arr[i] > arr[i+1] > ... > arr[arr.length - 1] 给你由整数组成的山脉数组 arr ，返回任何满足 arr[0] <
 * arr[1] < ... arr[i - 1] < arr[i] > arr[i + 1] > ... > arr[arr.length - 1] 的下标 i 。
 *
 * <p>来源：力扣（LeetCode） 链接：https://leetcode-cn.com/problems/peak-index-in-a-mountain-array
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 */
public class Test852 {

    public int peakIndexInMountainArray(int[] arr) {
        int high = arr.length-1;
        int low = 0;
        while (low<=high){
            int mid = low + (high -low) /2 ;
            if (mid!= 0 && mid != arr.length-1 &&arr[mid] > arr[mid+1] && arr[mid] > arr[mid -1]) return mid;
            if (mid == 0){
                low = mid +1;
                continue;
            }
            if (mid == arr.length -1){
                high = mid -1;
                continue;
            }
            if (arr[mid] <arr[mid -1]) high =mid -1;
            if (arr[mid] < arr[mid+1]) low = mid +1;
        }
        return -1;
    }
}


class Demo852{
  public static void main(String[] args) {
    int[] arr = {3,5,3,2,0};
    Test852 t = new Test852();
    System.out.println(t.peakIndexInMountainArray(arr));
  }
}